package com.zp.drug.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.bo.DrugSupplierCataBo;
import com.zp.drug.entity.vo.DrugSupplierCataVo;
import com.zp.drug.service.IDrugSupplierCataService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 药品供应商生产商维护控制器
 * 前端访问路由地址为:/drug/drugSupplierCata
 *
 * @author zhang peng
 * @ date 2023-05-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugSupplierCata")
public class DrugSupplierCataController extends BaseController {

    private final IDrugSupplierCataService iDrugSupplierCataService;

    /**
     * 查询药品供应商生产商维护列表 (分页)
     */
    @SaCheckPermission("base:drugSupplierCata:list")
    @GetMapping("/selectPage")
    public TableDataInfo<DrugSupplierCataVo> selectPage (DrugSupplierCataBo bo, PageQuery pageQuery) {
        return iDrugSupplierCataService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询药品供应商生产商维护列表 (不分页)
     */
    @SaCheckPermission("base:drugSupplierCata:list")
    @GetMapping("/list")
    public R<List<DrugSupplierCataVo>> list (DrugSupplierCataBo bo) {
        List<DrugSupplierCataVo> list = iDrugSupplierCataService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品供应商生产商维护列表
     */
    @SaCheckPermission("base:drugSupplierCata:export")
    @Log(title = "药品供应商生产商维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (DrugSupplierCataBo bo, HttpServletResponse response) {
        List<DrugSupplierCataVo> list = iDrugSupplierCataService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品供应商生产商维护", DrugSupplierCataVo.class, response);
    }

    /**
     * 获取药品供应商生产商维护详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:drugSupplierCata:list")
    @GetMapping("/{id}")
    public R<DrugSupplierCataVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugSupplierCataService.queryById(id));
    }

    /**
     * 新增药品供应商生产商维护
     */
    @SaCheckPermission("base:drugSupplierCata:add")
    @Log(title = "药品供应商生产商维护", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<DrugSupplierCataBo> add (@Validated(AddGroup.class) @RequestBody DrugSupplierCataBo bo) {
        return R.ok(iDrugSupplierCataService.insertByBo(bo));
    }

    /**
     * 修改药品供应商生产商维护
     */
    @SaCheckPermission("base:drugSupplierCata:edit")
    @Log(title = "药品供应商生产商维护", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody DrugSupplierCataBo bo) {
        return toAjax(iDrugSupplierCataService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品供应商生产商维护
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:drugSupplierCata:remove")
    @Log(title = "药品供应商生产商维护", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iDrugSupplierCataService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    @GetMapping("/refreshCacheDrugSupplierCata")
    public R<String> refreshCacheDrugSupplierCata () {
        iDrugSupplierCataService.refreshCacheDrugSupplierCata();
        return R.ok("刷新成功");
    }
}
